package planner.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import planner.domain.User;
import planner.exception.UserDuplicateException;
import planner.service.UserService;

public class JoinController extends SimpleFormController {
	private UserService userService = null;
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		User user = (User) command;
		try {
			// TODO: 빈칸
			userService.join(user);
		} catch (UserDuplicateException e) {
//			errors.reject("duplicated.userId");
			errors.reject("required.userId");
			return super.showForm(request, response, errors);
		}
		return new ModelAndView(getSuccessView(), "user", user);
	}
}
